<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\index\service\ApiService;
use app\service\CipherService;
use app\model\card\Card as CardModel;
use app\model\card\CardBig as CardBigModel;
use app\model\card\CardMod as CardModModel;
use app\model\card\CardCate as CardCateModel;
use think\facade\View;

class Api extends Base
{
    /**
     * 绑定秘钥
     */
    public function binding(ApiService $apiService,CipherService $cipherService)
    {
        $param = file_get_contents("php://input");
        if(!$param){
            return jsonReturn(1, '请提交参数',[]);
        }
        $param = json_decode($param,true);
        if(!$param['keys']){
            return jsonReturn(1, '密钥不能为空',[]);
        }
        
        $info = $apiService->binding($param['keys'],$param['device_num'],$param['user_num'],$param['ip']);
        if($info['code'] == 0){
            return jsonReturn(0, '绑定成功',[]);
        }else{
            return jsonReturn(1, '绑定失败,'.$info['msg'],[]);
        } 
    }
    /**
     * 解除秘钥绑定
     */
    public function unbinding(ApiService $apiService,CipherService $cipherService)
    {
        $param = file_get_contents("php://input");
        if(!$param){
            return jsonReturn(1, '请提交参数',[]);
        }
        $param = json_decode($param,true);
        if(!$param['keys']){
            return jsonReturn(1, '密钥不能为空',[]);
        }
        if(!$param['unkeys']){
            return jsonReturn(1, '操作密钥不能为空',[]);
        }
        $info = $apiService->unbinding($param['keys'],$param['unkeys'],$param['ip']);
        if($info['code'] == 0){
            return jsonReturn(0, '解绑成功',[]);
        }else{
            return jsonReturn(1, '解绑失败,'.$info['msg'],[]);
        } 
    }
    
    /**
     * 获取平台秘钥
     */
    public function get(CipherService $cipherService,CardModel $cardModel,CardBigModel $cardBigModel,CardModModel $cardModModel,CardCateModel $cardCateModel)
    {
        $param = file_get_contents("php://input");
        if(!$param){
            json(array('code'=>1,'msg'=>'请提交参数'));
        }
        $param = json_decode($param,true);
        if(!$param['keys']){
            json(array('code'=>1,'msg'=>'密钥不能为空'));
        }
        
        $info = $cardModel->findOne(['is_del'=>0,'keys' => $param['keys']])['data'];
        
        if(!$info){
            json(array('code'=>1,'msg'=>'卡密不存在'));
        }

        if($info['remain_price']<=0){
            json(array('code'=>1,'msg'=>'余额不够，请充值'));
        }
        $cardCate = $cardCateModel->where(['id' => $info['cate_id']])->find();
        if(!$cardCate){
            json(array('code'=>1,'msg'=>'平台分类不存在'));
        }
        $biginfo = $cardBigModel->where(['is_del'=>0,'id' => $info['big_id']])->find();
        if(!$biginfo){
            json(array('code'=>1,'msg'=>'平台卡密不存在'));
        }

       
        
        $cardmod = $cardModModel->where(['cate_id'=>$biginfo['cate_id'],'status'=>1,'is_del'=>0])->select();
        if(!$cardmod){
            json(array('code'=>1,'msg'=>'模型不存在'));
        }
        $key = $info['keys'];
        $str = $biginfo['keys'];
        
        $enkey = $cipherService->key_encrypt($key,$str);
        return jsonReturn(0, '获取成功',['apiurl'=>$cardCate['apiurl'],'mod'=>$cardmod,'adtitle'=>'','enkey'=>$enkey,'keys'=>$key,'remain_price'=>$info['remain_price']]);
        //print_r($data);
        //print_r("<br>");
        //$data2 = $cipherService->key_decrypt($key,$data);
        //print_r($data2);
    }
    /**
     * 信息处理完后的回调
     */
    public function notify(CardModel $cardModel,CardModModel $cardModModel)
    {
        $param = file_get_contents("php://input");
        if(!$param){
            json(array('code'=>1,'msg'=>'请提交参数'));
        }
        $param = json_decode($param,true);
        if(!$param['keys']){
            json(array('code'=>1,'msg'=>'密钥不能为空'));
        }
        if(!$param['mod']){
            json(array('code'=>1,'msg'=>'模型不能为空'));
        }
        if(!$param['title']){
            json(array('code'=>1,'msg'=>'标题不能为空'));
        }
        if(!$param['content']){
            json(array('code'=>1,'msg'=>'内容不能为空'));
        }
        $error = 0;
        $times = date('Y-m-d H:i:s');
        $info = $cardModel->findOne(['keys' => $param['keys']])['data'];
        if(!$info){
            $error = $error + 1;
            $info['cate_id'] = '2';
        }
        $mod = $cardModModel->findOne(['cate_id' => $info['cate_id'],'name' => $param['mod']])['data'];
        if(!$mod){
            $error = $error + 100;
            $mod = $cardModModel->findOne(['cate_id' => $info['cate_id'],'name' => 'gpt-3.5-turbo'])['data'];
        }
        //计算费用
        if($mod['price_type'] ==1){
            $lang = 1000;
            $alltext = $param['title'].
            $leng = mb_strlen($param['title'].$param['content'], "UTF-8");
            $price = -(ceil($leng / 1000) * $mod['price']);
        }elseif($mod['price_type'] ==2){
            $price = -$mod['price'];
        }elseif($mod['price_type'] ==3){
            $price = -$mod['price'];
        }
        $logid = $this->app->db->name('card_log')->insertGetId(['type_id'=>1,'mod'=>$param['mod'],'card_id'=>$info['id'],'keys'=>$info['keys'],'price'=>$price,'pre_price'=>$info['remain_price'],'ip'=>'','create_time'=>$times]);
        if($logid){
            $cardModel->where(['id' => $info['id']])->inc('remain_price',$price)->update();
            $this->app->db->name('card_problem')->insertGetId(['card_log_id'=>$logid,'card_id'=>$info['id'],'title'=>$param['title'],'content'=>$param['content'],'keys'=>$info['keys'],'create_time'=>$times]);
        }else{
            //如果有错误是这样的
            $error = $error+10000;
            $this->app->db->name('card_problem')->insertGetId(['card_log_id'=>0,'card_id'=>$info['id'],'title'=>$param['title'],'content'=>$param['content'],'keys'=>$info['keys'],'create_time'=>$times]);
        }
        if($error > 0){
            $desc = "用户问答回调API信 息出错";
            $this->app->db->name('error')->insertGetId(['a_id'=>$info['id'],'errcode'=>$error,'desc'=>$desc,'keys'=>$info['keys'],'create_time'=>$times]);
        }
        echo "succress";
    }
}